package impl

import (
	"context"
	"errors"
	"wms/entity"
	"wms/glo"
	"wms/pb"
	"wms/service/request"
	"wms/util"
)

type simService struct {
}

var SimSrvImpl simService

func (s *simService) List(ctx context.Context, arg *request.QuerySimRequest) ([]*pb.SimInfo, error) {
	var entities []*entity.SimInfo
	_db := glo.Db_wms.Model(&entity.SimInfo{})
	//
	if len(arg.ICCID) > 0 {
		// _db = _db.Where("dict_name LIKE ?", "%"+arg.ICCID+"%")
		_db = _db.Where("ICCID LIKE ?", "%"+arg.ICCID+"%")
	}
	if len(arg.MSISDN) > 0 {
		_db = _db.Where("MSISDN LIKE ?", "%"+arg.MSISDN+"%")
	}
	// if err := _db.Order("sim_id desc").Find(&entities).Error; err != nil {
	if err := _db.Find(&entities).Error; err != nil {
		return nil, errors.New("sql err" + err.Error())
	}
	//
	modelListOut := make([]*pb.SimInfo, 0, len(entities))
	for _, tmp := range entities {
		_w := new(pb.SimInfo)
		util.CopyStruct(_w, tmp)
		modelListOut = append(modelListOut, _w)
	}
	return modelListOut, nil
}
